﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using DavuxLib;

namespace CamShared
{
    public partial class SettingsViewer : Form
    {
        public SettingsViewer()
        {
            InitializeComponent();
            lsv.SmallImageList = imageList1;
            LoadListView(null);
            lsv.View = View.Details;
            lsv.FullRowSelect = true;
            Settings.OnSettingChanged += new DavuxLib.Settings.SettingChanged(Settings_OnSettingChanged);


            ContextMenu c = new ContextMenu();

            MenuItem mi = new MenuItem();
            mi.Text = "About CamServer...";
            mi.Click += new EventHandler(About_Click);
            c.MenuItems.Add(mi);

            mi = new MenuItem();
            mi.Text = "Settings...";
            mi.Click += new EventHandler(Settings_Click);
            c.MenuItems.Add(mi);

            mi = new MenuItem();
            mi.Text = "-";
            c.MenuItems.Add(mi);

            mi = new MenuItem();
            mi.Text = "Exit";
            mi.Click += new EventHandler(mi_Click);
            c.MenuItems.Add(mi);

            NI.ContextMenu = c;
            NI.Visible = false;
            NI.DoubleClick += new EventHandler(NI_DoubleClick);

            tmr.Interval = 500;
            tmr.Tick += new EventHandler(tmr_Tick);
        }

        void tmr_Tick(object sender, EventArgs e)
        {
            LoadListView(null);
            tmr.Enabled = false;
        }

        public delegate void ExitRequested();
        public event ExitRequested OnExit;

        public event ExitRequested OnAbout;

        delegate void SettingChanged(ConfValue c);

        void Settings_OnSettingChanged(ConfValue c)
        {
            if (lsv.InvokeRequired)
            {
                lsv.Invoke(new SettingChanged(Settings_OnSettingChanged), c);
            }
            else
            {
                tmr.Stop();
                tmr.Start();
            }
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            LoadListView(txtSearch.Text);
        }

        void LoadListView(string s)
        {
            if (s != null)
            {
                s = s.ToLower();
            }
            lsv.Items.Clear();
            ListViewGroup g = new ListViewGroup();
            g.Header = "Global Settings";
            lsv.Groups.Add(g);

            for(int i = Settings.Dict.Count -1 ; i >= 0; i--)
            {
                ConfValue c = Settings.Dict[i];
                if (!c.Key.StartsWith("#"))
                {
                    if (s == null || c.Key.ToLower().IndexOf(s) > -1 || c.Value.ToLower().IndexOf(s) > -1)
                    {
                        ListViewItem lvi = new ListViewItem();
                        lvi.Text = c.Key;
                        lvi.SubItems.Add(new ListViewItem.ListViewSubItem(lvi, c.Value));
                        lvi.Tag = c;
                        //lvi.ImageIndex = c.RestartRequired ? 0 : -1;

                        if (c.Key.IndexOf("|") > -1)
                        {
                            foreach (ListViewGroup lvg in lsv.Groups)
                            {
                                if (lvg.Header == c.Key.Substring(0, c.Key.IndexOf("|")))
                                {
                                    lvi.Group = lvg;
                                    lvi.Text = c.Key.Substring(c.Key.IndexOf("|") + 1);
                                    break;
                                }
                            }

                            if (lvi.Group == null)
                            {
                                ListViewGroup lvg = new ListViewGroup();
                                lvg.Header = c.Key.Substring(0, c.Key.IndexOf("|"));
                                lsv.Groups.Add(lvg);
                                lvi.Group = lvg;
                                lvi.Text = c.Key.Substring(c.Key.IndexOf("|") + 1);
                            }
                        }

                        if (lvi.Group == null)
                        {
                            lvi.Group = lsv.Groups[0];
                        }
                        lsv.Items.Add(lvi);
                    }
                }
            }
            lsv.Sorting = SortOrder.Ascending;
            lsv.Sort();
        }

        private void lsv_DoubleClick(object sender, EventArgs e)
        {
            foreach (ListViewItem lvi in lsv.SelectedItems)
            {
                Input inp = new Input("Enter Value for " + lvi.Text, lvi.SubItems[1].Text, "Modify Setting");

                if (inp.ShowDialog() == DialogResult.OK)
                {
                    ConfValue c = (ConfValue)lvi.Tag;
                    if (c.Value != inp.TextValue)
                    {
                        //if (c.RestartIsRequired)
                       // {
                        //    c.RestartRequired = true;
                       // }
                    }
                    c.Value = inp.TextValue;
                    inp = null;
                    Settings.Save();
                    LoadListView(txtSearch.Text == "" ? null : txtSearch.Text);
                }
                return;
            }
        }

        private void SettingsViewer_FormClosing(object sender, FormClosingEventArgs e)
        {
            //e.Cancel = true;
            //Hide();
        }


        void mi_Click(object sender, EventArgs e)
        {
            if (OnExit != null)
            {
                OnExit();
            }
            NI.Visible = false;
            Application.DoEvents();
            Environment.Exit(75);
        }

        void Settings_Click(object sender, EventArgs e)
        {
            Show();
        }

        void About_Click(object sender, EventArgs e)
        {
            if (OnAbout != null)
            {
                OnAbout.Invoke();
            }
            //MessageBox.Show("CamServer by Dave Amenta (DaveAmenta@Gmail.com)", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        void NI_DoubleClick(object sender, EventArgs e)
        {
            Show();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            try
            {
                if (OnExit != null)
                {
                    OnExit();
                }
                NI.Visible = false;
                Application.DoEvents();
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Error OnExit: " + ex);
            }
            //System.Diagnostics.Process.Start(Application.ExecutablePath);
            Environment.Exit(12);
        }
    }
}
